package com.delamer.facile.exe;

import com.delamer.utils.MsgUtil;
import com.delamer.utils.PermitUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ExecutorLocation {
    public void help(CommandSender sender, String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Bukkit.getPluginManager().getPlugin("DelamerFacile").getResource("location.help"), "UTF-8"));
        List<String> results = new ArrayList<String>();
        String line = null;
        while ((line = bufferedReader.readLine()) != null)
            results.add(line);
        sender.sendMessage(results.toArray(new String[]{}));
    }

    public void info(CommandSender sender, String[] args) {
        if (args.length > 1 && !sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令查看其他玩家位置"));
            return;
        }
        if (!PermitUtil.hasPermit(sender.getName(), "location")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        Player target = null;
        if (args.length == 1) {
            if (sender instanceof Player) {
                target = (Player) sender;
            }
        } else {
            target = Bukkit.getPlayerExact(args[1]);
        }
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        String worldName = target.getWorld().getName();
        if (Bukkit.getPluginManager().getPlugin("DelamerFacile").getConfig().contains("world-name-replace." + worldName))
            worldName = Bukkit.getPluginManager().getPlugin("DelamerFacile").getConfig().getString("world-name-replace." + worldName);
        Location location = target.getLocation();
        String[] msg = {
                "§e玩家：§f" + target.getName(),
                "§e世界：§f" + worldName,
                "§e坐标：§f" + (int) location.getX() + " , " + (int) location.getY() + " , " + (int) location.getZ()
        };
        sender.sendMessage(msg);
    }

    public void send(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if (!PermitUtil.hasPermit(sender.getName(), "location")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        Player target = null;
        if (args.length > 1)
            target = Bukkit.getPlayerExact(args[1]);
        Player player = (Player) sender;
        String worldName = player.getWorld().getName();
        if (Bukkit.getPluginManager().getPlugin("DelamerFacile").getConfig().contains("world-name-replace." + worldName))
            worldName = Bukkit.getPluginManager().getPlugin("DelamerFacile").getConfig().getString("world-name-replace." + worldName);
        Location location = player.getLocation();
        String[] msg = {
                "§e玩家：§f" + player.getName(),
                "§e世界：§f" + worldName,
                "§e坐标：§f" + (int) location.getX() + " , " + (int) location.getY() + " , " + (int) location.getZ()
        };
        if (target != null) {
            target.sendMessage("§e玩家§b" + player.getName() + "§e向你共享了他的位置");
            target.sendMessage(msg);
            player.sendMessage(MsgUtil.msg("发送成功，目标玩家已收到你当前的位置"));
        } else {
            for (Player playerTemp : Bukkit.getOnlinePlayers())
                playerTemp.sendMessage("§e玩家§b" + player.getName() + "§e向你共享了他的位置\n" + msg[0] + "\n" + msg[1] + "\n" + msg[2]);
            player.sendMessage(MsgUtil.msg("发送成功，全体玩家已收到你当前的位置"));
        }
    }
}
